<?php
namespace app\index\service;


use app\index\model\DeptModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowCourseModel;
use app\index\model\MenuButModel;
use app\index\model\MenuModel;
use app\index\model\OptionModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;

class UserService extends CommonService
{

    public function get_dept_type(){
        return [
            1=>'部门',
            2=>'单位',
        ];
    }

    //部门信息列表页-获取全部部门信息
    public function get_deptinfo_all($pid=0){
        $data = DeptModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['fuer_name'] = UserModel::get_realname_by_pk($item['fuer']);
            $item['children'] = $this->get_deptinfo_all($item['id']);
        }
        return $data;
    }

    //获取全部单位信息
    public function get_chaninfo_all($pid=0){
        $data = DeptModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid,'type'=>2])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['fuer_name'] = UserModel::get_realname_by_pk($item['fuer']);
            $item['children'] = $this->get_chaninfo_all($item['id']);
        }
        return $data;
    }

//    //获取机构及其子级机构id
//    public function get_chansonid($id){
//        $data = DeptModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid,'type'=>2])->order('sort ASC')->select()->toArray();
//        foreach ($data as &$item){
//            $item['fuer_name'] = UserModel::get_realname_by_pk($item['fuer']);
//            $item['children'] = $this->get_chaninfo_all($item['id']);
//        }
//        return $data;
//    }

    public function get_chansonid($id){
        $list[] = $id;
        $seaarr = [$id];

        while (count($seaarr) > 0) {
            $son = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'type'=>2])
                ->where('pid','in',$seaarr)->column('id');
            $seaarr = [];
            foreach ($son as $v){
                $list[] = $v;
                $seaarr[] = $v;
            }
        }
        return $list;
    }



    public function check_dept_name($name, $id=0){
        return !!DeptModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }

    //岗位信息列表页-获取全部岗位信息
    public function get_usergang_all($pid=0){
        $data = UserGangModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_usergang_all($item['id']);
        }
        return $data;
    }



    public function check_usergang_name($name, $id=0){
        return !!UserGangModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }


    public function get_list_user_dept($pid=0){
        $data = DeptModel::field('id,name as title')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['spread'] = true;
            $item['children'] = $this->get_list_user_dept($item['id']);
        }
        return $data;
    }


    public function check_user_name($name, $id=0){
        return !!UserModel::where(['comid'=>$this->userinfo['comid'],'realname'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }

    public function check_login_tel($tel, $id=0){
        return !!UserModel::where(['tel'=>$tel, 'deleted_user'=>0])->where('id','neq', $id)->find();

    }


    public function get_option_nav(){
        return [
            'cert' => '证书模块',
            'bid' => '投标模块',
            'engin' => '项目模块',
            'xian' => '现场模块',
            'cai' => '财务模块',
            'qi' => '其他',
        ];
    }

    public function get_flowset_nav(){
        return [
            'engin' => '项目',
            'bid' => '投标',
            'cert' => '证书',
//            'xian' => '现场',
            'cai' => '财务',
            'other' => '其他',
        ];
    }


    public function get_optioninfo_all($pid=0){
        $data = OptionModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_optioninfo_all($item['id']);
        }
        return $data;
    }

    public function check_option_name($name, $topid=0, $id=0){
        return !!OptionModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0, 'topid'=>$topid])
            ->where('id','neq', $id)->find();
    }

    public function check_option_keyid($keyid, $topid=0, $id=0){
        return !!OptionModel::where(['comid'=>$this->userinfo['comid'],'keyid'=>$keyid,'deleted_user'=>0, 'topid'=>$topid])
            ->where('id','neq', $id)->find();
    }


    public function check_userrole_name($name, $id=0){
        return !!UserRoleModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }


    public function get_menuinfo_all(){
        $data = MenuModel::field('*,id as value')->where(['pid'=>0])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = MenuModel::field('*,id as value')->where(['pid'=>$item['id']])->order('sort ASC')->select()->toArray();
            $item['but'] = [];
            foreach ($item['children'] as &$item22){
                $item22['children'] = MenuModel::field('*,id as value')->where(['pid'=>$item22['id']])->order('sort ASC')->select()->toArray();
                if($item22['modedir']){
                    $item22['but'] = MenuButModel::where(['modedir'=>$item22['modedir'],'status'=>1])->order('sort ASC')->select()->toArray();
                }else{
                    $item22['but'] = [];
                }
                foreach ($item22['children'] as &$item33){
                    if($item33['modedir']){
                        $item33['but'] = MenuButModel::where(['modedir'=>$item33['modedir'],'status'=>1])->order('sort ASC')->select()->toArray();
                    }else{
                        $item33['but'] = [];
                    }
                }
            }
        }
        return $data;
    }

    public function get_role_data_auth(){
        return [
            'self' => '本人数据',
            'dept' => '本部门数据',
            'deptmore' => '本部门及以下数据',
            'mechan' => '本单位数据',
            'mechanmore' => '本单位及以下数据',
            'all' => '全部数据',
        ];
    }


    public function get_user_chanid($deptid=0){
        $chanid = 0;
        if(!$deptid){
            $deptid = $this->userinfo['deptid'];
        }
        while ($chanid < 1) {
            $dept = DeptModel::where(['id'=>$deptid])->find();
            if($dept['type'] == 2){
                $chanid = $dept['id'];
            }else{
                $deptid = $dept['pid'];
            }
        }
        return $chanid;
    }

    public function get_dept_chanid($deptid){
        $chanid = 0;
        while ($chanid < 1) {
            $dept = DeptModel::where(['id'=>$deptid])->find();
            if($dept['type'] == 2){
                $chanid = $dept['id'];
            }else{
                $deptid = $dept['pid'];
            }
        }
        return $chanid;
    }

    public function create_course_vernum($classid=0){
        $num = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid])->count();
        return 'V'.($num+1).'（'.date("Y-m-d").'）';
    }


    public function add_default_course($classid=0){
        $data = [
            'comid'=>$this->userinfo['comid'],
            'status'=>0,
            'classid'=>$classid,
            'vernum'=>$this->create_course_vernum($classid),
            'step'=>json_encode([]),
            'created_user'=>$this->userinfo['id'],
            'updated_user'=>$this->userinfo['id'],
            'created_at'=>date("Y-m-d H:i:s"),
            'updated_at'=>date("Y-m-d H:i:s"),
        ];
        return FlowCourseModel::insertGetId($data);
    }


    function get_flowrelas_info($flowrelas=''){
        $data = FlowBillModel::field('id,sericnum,modename')->where('id','in',$flowrelas)->select()->toArray();
        return $data;
    }
    function get_users_part_arr($datastr='', $type='part'){
        $data = [];

        if($type == 'user'){
            $info = UserModel::field('id,realname as name')->where('id','in', $datastr)->where(['deleted_user'=>0])->select()->toArray();
            foreach ($info as $v){
                $v['part'] = 'user';
                $data[] = $v;
            }
        }

        if($type == 'dept'){
            $info = DeptModel::field('id,name')->where('id','in', $datastr)->where(['deleted_user'=>0])->select()->toArray();
            foreach ($info as $v){
                $v['part'] = 'dept';
                $data[] = $v;
            }
        }

        if($type == 'part'){
            $arr = explode(',', $datastr);
            foreach ($arr as $item){
                if(strpos($item, 'dept') !== false){
                    $id = (int)str_replace('dept', '', $item);
                    $info = DeptModel::field('id,name')->where(['id'=>$id,'deleted_user'=>0])->find()->toArray();
                    if($info){
                        $info['part'] = 'dept';
                        $data[] = $info;
                    }
                }
                if(strpos($item, 'user') !== false){
                    $id = (int)str_replace('user', '', $item);
                    $info = UserModel::field('id,realname as name')->where(['id'=>$id,'deleted_user'=>0])->find()->toArray();
                    if($info){
                        $info['part'] = 'user';
                        $data[] = $info;
                    }
                }
            }
        }

        return $data;
    }


    public function get_flow_term_level(){
        return [1,2,3,4,5];
    }

    public function get_flow_term_conder(){
        return [
            'like' => '包含',
            'notlike' => '不包含',
            'eq' => '等于',
            'neq' => '不等于',
            'gt' => '大于',
            'egt' => '大于等于',
            'lt' => '小于',
            'elt' => '小于等于'
        ];
    }

    public function get_flow_shen_type(){
        return [
            'user' => '用户',
            'role' => '角色',
            'dept' => '部门负责人',
            'gang' => '岗位',
            'base' => '系统',
        ];
    }

    public function get_flow_shen_type_start(){
        return [
            '' => '全部',
            'user' => '用户',
            'role' => '角色',
            'dept' => '部门',
            'gang' => '岗位',
        ];
    }

    public function get_flow_base(){
        return [
            'creater' => '申请人',
            'credepter' => '申请人部门负责人',
            'fuzer' => '投标负责人',
            'booker' => '标书制作人', //str
            'man_xiang' => '项目负责人',
            'man_zhuan' => '项目专管员', //str
            'man_zi' => '资料专员', //str
            'man_cheng' => '成本专员', //str
            'man_zhian' => '质安专员', //str
        ];
    }

    function get_users_part_str($datastr='', $shentype=''){
        $str = '';
        if(!$datastr){
            return $str;
        }

        if($shentype == 'user'){
            $res = UserModel::where('id','in',$datastr)->where(['deleted_user'=>0])->column('realname');
            $str = implode(',', $res);
        }elseif($shentype == 'dept'){
            $res = DeptModel::where('id','in',$datastr)->where(['deleted_user'=>0])->column('name');
            $str = implode(',', $res);
        }elseif($shentype == 'role'){
            $res = UserRoleModel::where('id','in',$datastr)->where(['deleted_user'=>0])->column('name');
            $str = implode(',', $res);
        }elseif($shentype == 'gang'){
            $res = UserGangModel::where('id','in',$datastr)->where(['deleted_user'=>0])->column('name');
            $str = implode(',', $res);
        }elseif($shentype == 'base'){
            $baseArr = $this->get_flow_base();
            $str = isset($baseArr[$datastr]) ? $baseArr[$datastr] : '';
        }

        return $str;
    }



    public function put_course_step_info($data, $factorArr){
        $startArr = [];
        $shengArr = [];
        if(count($data) == 0){
            $startArr = ['node'=>'start','data'=>['users'=>'']];
        }else{
            foreach ($data as $k=>$v){
                if($k == 0){
                    if(isset($v['data'])){
                        $v['data']['users'] = isset($v['data']['users']) ? $v['data']['users'] : '';
                        $startArr = $v;
                    }else{
                        $startArr = ['node'=>'start','data'=>['users'=>'']];
                    }
                }else{
                    $shengArr[] = $v;
                }
            }
        }
        $shengArr = $this->put_course_step_info_completion($shengArr, $factorArr);
        return ['startnode'=>$startArr, 'steparr'=> $shengArr];
    }


    public function put_course_step_info_completion(&$data, $factorArr){
        foreach ($data as &$item){
            if($item['node'] == 'rover'){
                $item['users_name'] = $this->get_users_part_str($item['data']['users'], $item['data']['shentype']);
            }else{
                foreach ($item['data'] as &$vv){
                    $vv['list_name'] = '无条件';
                    if($vv['list']){
                        $vv['list_name'] = $this->get_term_list_factor($vv['list'], $factorArr);
                    }
                    if(isset($vv['children']) && $vv['children']){
                        $vv['children'] = $this->put_course_step_info_completion($vv['children'], $factorArr);
                    }
                }
            }
        }

        return $data;
    }


    public function get_term_list_factor($list, $factorArr){
        $tiao = $this->get_flow_term_conder();
        $facArr = [];
        foreach ($factorArr as $item){
            $facArr[$item['name']] = $item;
        }

        $str = '';

        foreach ($list as $v){
            $_str = '';
            foreach ($v as $vv){
                if(isset($facArr[$vv['fieldname']])){
                    $fieldtype = $tiao[$vv['fieldtype']];
                    if($_str){
                        $_str .= '，'.$facArr[$vv['fieldname']]['title'].' '.$fieldtype.' '.$vv['fieldvalue'];
                    }else{
                        $_str .= $facArr[$vv['fieldname']]['title'].' '.$fieldtype.' '.$vv['fieldvalue'];
                    }
                }
            }
            if($str){
                $str .= '<br/>或<br/>'.$_str;
            }else{
                $str = $_str;
            }
        }
        return $str;
    }


    public function check_flow_is_rover($data){
        $status = false;
        if(is_array($data)){
            foreach ($data as $v){
                if($v['node'] == 'rover'){
                    $status = true;
                    continue;
                }
                if($v['node'] == 'term'){
                    $status = $this->check_flow_is_rover_child($v['data']);
                }
            }
        }

        return $status;
    }

    public function check_flow_is_rover_child($data){
        foreach ($data as $v){
            foreach ($v['children'] as $vv){
                if($vv['node'] == 'term'){
                    return $this->check_flow_is_rover_child($vv['data']);
                }
                if($vv['node'] == 'rover'){
                    return true;
                }
            }
        }
        return false;
    }


    public function get_user_role_type_menus(){
        return [
            1 =>[],
            2 =>[6,37,38,39,40,334,68,69,70,339,340,294,194,78,195,197,198,199,296,297,125,126,132,136,72,137,138,140,141,142,81,338,87,382,383,90,341,82,96,97,98,379,380,83,100,101,102,103,84,94,95,85,107,108,86,233,234,104,105,238,235,236,237,106,354,355,356,110,357,111,112,113],
            3 =>[6,37,38,40,334,68,69,70,339,340,294,194,78,195,197,198,199,296,297,125,126,132,136,72,137,138,140,141,142,81,338,87,382,383,90,341,82,96,97,98,379,380,83,100,101,102,103,84,94,95,85,107,108,86,233,234,104,105,238,235,236,237,106,354,355,356,110,357,111,112,113],
            4 =>[6,68,70,125,126,81,338,87,93,90,82,96,97,98,380,83,100,101,102,103,84,94,95,8,129,240],
            5 =>[6,38,39,40,334,68,69,70,339,340,294,194,78,195,197,198,199,296,297,125,126,132,136,72,137,138,140,141,142,81,338,87,382,383,90,341,82,96,97,98,99,378,379,380,83,100,101,102,103,84,94,95,85,107,108,243,86,233,234,104,105,238,235,236,237,106,354,355,356,110,357,111,112,113,5,42,321,322,43,324,307,306,325,44,333,308,328,329,45,148,149,150,47,4,28,211,213,212,384,385,386,310,317,29,214,331,31,215,216,32,217,218,33,219,221,34,223,35,8,76,123,124,231,232,350,121,133,139,134,135,196,348,349,122,191,192,193,312,313,143,144,145,146,147,129,130,131,239,240,241,242,75,117,118,127,128,152,2,114,169,170,171,172,173,164,165,166,167,168,181,175,177,178,179,180,187,188,189,190,186,185,3,153,315,154,344,210,345,159,160,161,162,163,155,156,157,158,346,347,244,245,255,256,257,258,246,259,260,261,262,263,264,265,247,269,267,295,268,266,270,271,248,272,273,249,275]
        ];
    }
    public function get_user_role_type_buts(){
        return [
            1 =>[],
            2 =>[88,118,91,115,116,117,125,128,129,131,133,134,135,136,139,143,439,156,159,274,475,119,294,299,305,556,163,656,692,662,665,693,663,664,666,669,694,667,668,175,189,195,192,649,650,652,198,204,205,206,207,210,183,186,498,483,484,485,486,487,488,225,213,489,216,570,573,577,228,229,230,231,232,233,234],
            3 =>[125,128,129,131,132,133,134,135,136,139,143,439,156,159,274,475,294,299,305,556,163,656,692,662,665,693,663,664,666,669,694,667,668,175,189,195,192,649,650,652,198,204,205,206,207,210,183,186,498,483,486,225,213,489,216,570,573,577,228,229,231,232,234],
            4 =>[274],
            5 =>[88,89,118,91,114,115,116,117,125,126,128,129,131,132,133,134,135,136,139,453,143,439,454,156,159,274,475,119,452,655,291,292,293,294,299,305,556,163,656,692,662,665,693,663,664,666,669,694,667,668,175,189,195,192,646,647,648,643,644,645,653,649,650,651,652,198,204,205,206,207,210,183,186,219,220,221,500,222,223,224,498,499,495,496,497,501,502,483,484,485,486,487,488,225,213,489,216,570,573,576,578,579,580,577,228,229,230,231,232,233,234,237,238,239,95,96,97,98,543,544,545,555,468,671,672,673,674,675,549,550,325,328,469,470,471,69,70,71,72,73,661,74,510,511,551,552,553,554,76,267,77,78,79,81,82,83,84,85,87,505,92,251,337,449,450,451,261,262,263,264,265,266,472,473,474,476,477,478,479,277,278,297,298,280,281,284,286,287,288,443,444,445,149,150,151,428,429,430,433,434,435,302,303,304,315,316,317,321,322,313,268,455,271,272,273,492,493,494,122,258,259,260,334,335,336,240,241,246,243,245,242,382,385,386,387,391,392,393,388,389,390,371,374,375,376,377,378,379,406,407,408,409,410,411,412,394,395,396,397,398,399,400,403,404,405,419,420,421,422,424,426,427,416,417,418,3,4,5,62,63,64,65,66,68,342,343,345,561,503,504,564,359,362,365,368,346,350,354,356,567],
        ];
    }
    public function get_user_role_type(){
        return [
            1=>'公司人员角色',
            2=>'项目人员角色',
            3=>'项目联营人角色',
            4=>'供应商角色',
            5=>'分公司人员角色',
        ];
    }
    public function get_user_role_type_name($type){
        $arr = $this->get_user_role_type();
        $str = isset($arr[$type]) ? $arr[$type] : '';
        return $str;
    }

    public function get_user_roleArr(){
        $roleArr = UserRoleModel::field('*,id as value')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->order('sort ASC')->select()->toArray();
        foreach ($roleArr as &$item){
            $item['type_name'] = $this->get_user_role_type_name($item['type']);
            $item['name'] = $item['name'].' 【'. $item['type_name'].'】';
        }
        return $roleArr;
    }



    public function get_user_take_type(){
        return [
            1=>'流程申请人交接',
            2=>'流程审批人交接',
        ];
    }
    public function get_user_take_type_name($type){
        $arr = $this->get_user_take_type();
        $str = isset($arr[$type]) ? $arr[$type] : '';
        return $str;
    }


    public function get_user_take_qing(){
        return [
            'qing_all'=>'所有申请',
            'qing_shen'=>'审批中申请',
        ];
    }
    public function get_user_take_pi(){
        return [
            'pi_all'=>'所有',
            'pi_pei'=>'流程配置',
            'pi_shen'=>'审批中流程',
        ];
    }
    public function get_user_take_shi_name($shi){
        $arr = [
            'qing_all'=>'所有申请',
            'qing_shen'=>'审批中申请',
            'pi_all'=>'所有',
            'pi_pei'=>'流程配置',
            'pi_shen'=>'审批中流程',
        ];
        $str = isset($arr[$shi]) ? $arr[$shi] : '';
        return $str;
    }




}
